﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Contra
{
    class Player : AnimatedSprite
    {
        public int hitPoints = 3;
      

        int frameSkip = 10;
        float charSpeed = 100;
        List<Bullet> allBullets ;

        ContentManager _content;

        public bool IsAlive
        {
            get
            {
                return hitPoints > 0;
            }
        }

        public void GotHit()
        {
            if (hitPoints > 0)
            {
                hitPoints--;
            }
        }

        public Player(Vector2 position, List<Bullet> bullets ) : base(position)
        {
            FramesPerSecond = 10;

            allBullets = bullets;
            AddAnimation(5,130, 0,"Left",23,37,new Vector2(0,0));

            AddAnimation(1, 0, 5, "StandingLeft", 23, 50, new Vector2(0, 0));


            AddAnimation(8, 130, 5, "Right", 23, 36, new Vector2(0, 0));

            AddAnimation(1,130, 9 , "StandingRight" , 23 ,37 , new Vector2(0 , 0));

            PlayAnimation("StandingLeft");

            
          }

        public void LoadContent(ContentManager content)
        {
            _content = content;
            sTexture = content.Load<Texture2D>("PlayerSprite");
        }


        public override void UpDate(GameTime gameTime)
        {
            sDirection = Vector2.Zero;
            HandleInput(Keyboard.GetState());
            float deltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
            sDirection *= charSpeed;
            sPosition += (sDirection * deltaTime);
            base.UpDate(gameTime);
            characterRec = new Rectangle((int)sPosition.X, (int)sPosition.Y, 23, 37);

        }


        public Bullet Shoot()
        {
            return BulletFactory.GetRegularBullet(_content, this);
        }



        private void HandleInput(KeyboardState keyState)
        {
            if (IsAlive)
            {


                if (keyState.IsKeyDown(Keys.W))
                {
                    sDirection += new Vector2(0, -1);
                }

                if (keyState.IsKeyDown(Keys.A))
                {
                    characterFacing = -5;
                    sDirection += new Vector2(-1, 0);
                    PlayAnimation("Left");
                    currentDir = myDirection.left;
                }

                if (keyState.IsKeyDown(Keys.S))
                {
                    sDirection += new Vector2(0, 1);

                }

                if (keyState.IsKeyDown(Keys.D))
                {
                    characterFacing = 5;
                    sDirection += new Vector2(1, 0);
                    PlayAnimation("Right");
                    currentDir = myDirection.right;


                }

                if (Keyboard.GetState().IsKeyDown(Keys.Space))
                {
                    if (frameSkip == 10)
                    {
                        allBullets.Add(Shoot());
                        frameSkip = 0;
                    }
                    else
                    {
                        frameSkip++;
                    }
                }            
                else
                {
                    if (characterFacing > 0)
                    {
                        PlayAnimation("StandingRight");
                    }
                    else
                    {
                        PlayAnimation("StandingLeft");
                    }
                    currentDir = myDirection.none;
                }
            }
        }

    }
}
